Kwik.Page.include("Kwik.Ui.Controls.WebBrowser.js");
Kwik.Page.include("Kwik.Ui.Controls.RichTextCommands.js");
Kwik.Page.include("Kwik.Ui.Xhtml.EmptyElements.js");
Kwik.Page.include("Kwik.Dom.Selection.js");

/**
* Editable HTMLIframe element wich can be used to create HTML content by executing markup commands on textselections. 
* @constructor 
* @extends Kwik.Ui.Controls.WebBrowser
* @requires Kwik.Ui.Controls.WebBrowser
* @requires Kwik.Delegate
* @requires Kwik.Ui.Xhtml.EmptyElements
* @requires Kwik.Dom.Selection
* @created 10:00 PM 8/7/2007
* @modified 7:53 PM 4/7/2008
*/
Kwik.Ui.Controls.RichTextBox = function(config)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Ui.Controls.WebBrowser");
	this.__class.push("Kwik.Ui.Controls.RichTextBox");

	this.textarea = null;
	this.lastSelection = null;

	/** Execute a command on the document */
	this.execute = function RichTextBox_execute(command, param)
	{
		if(!this.isReady){return;}
		if(this.fireEvent("onbeforeexecute", [command, param]) === true){return;}

		if(command in Kwik.Ui.Controls.RichTextCommands 
		&& Kwik.Ui.Controls.RichTextCommands[command].parameter === true
		&& typeof param == "undefined")
		{
			param = prompt();
		}
		
		var success = false;
		if(command == "ToggleSource")
		{
			this.toggleSource();
		}
		else
		{
			success = this.document.execCommand(command, false, param)
			this.document.body.focus();
			
		}

		if(success)
		{
			this.fireEvent("onchange"); 
			this.fireEvent("onexecute", [command, param]);
		}
	};

	/** Switch from or to source edit */
	this.toggleSource = function RichTextBox_toggleSource()
	{
		if(this.textarea.style.display == "none")
		{
			this.iframe.style.display = "none";
			this.textarea.style.display = "block";
			this.textarea.value = this.getXhtml();
		}
		else
		{

			this.textarea.style.display = "none";	
			this.iframe.style.display = "block";
			this.document.body.innerHTML = this.textarea.value;
		}
	};

	/** Set the element for this object and adds a hidden input to the document */
	this.setElement = function RichTextBox_setElement(element)
	{
		Kwik.Page.log.write("Kwik.Ui.Controls.RichTextBox.setElement: Setting the element and invoking enableEdit.");
		this.Kwik$Ui$Controls$WebBrowser.setElement(element);
		this.element.appendChild(this.textarea);
		setTimeout(this.referenceString() + ".enableEdit();", 500);
	};

	this._blur = function()
	{
		this.lastSelection = this.getSelection();
	};

	/** Event fired before execution of a command, might return true to cancel the process */
	this.onbeforeexecute = function(command, param){return false;}
	/** Event fired at execution of a command */
	this.onexecute = function(command, param){};
	/** Event fired when the data changes */
	this.onchange = function(){};
	/** Event fired when the iframe and content is loaded */
	this.onrichtextboxready = function(){};

	/** @ignore */
	this.main = function RichTextBox_main(config)
	{
		this.textarea = document.createElement("textarea");
		this.textarea.style.border = this.textarea.style.display = "none";
		this.textarea.style.width = this.textarea.style.height = "100%";

		this.attachEvent("onblur", this._blur);

		if(config){this.configure(config);}

	}; this.main(config);
};